package com.game.league;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;

import com.game.base.app.base.BaseActivity;
import com.game.base.app.base.BaseFragment;
import com.game.base.observe.MsgMgr;
import com.game.base.observe.MsgType;
import com.game.base.observe.PObserver;
import com.game.base.server.upgrade.UpgradeMgr;
import com.game.base.utils.time.IntervalUtil;
import com.game.league.articles.ArticlesFragment;
import com.game.league.databinding.ActivityMainBinding;
import com.game.league.market.MarketFragment;
import com.game.league.mine.MineFragment;
import com.game.league.task.TaskFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends BaseActivity<ActivityMainBinding> implements BottomNavigationView.OnNavigationItemSelectedListener, PObserver {
    private BaseFragment articlesFragment, mineFragment, taskFragment, marketFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        MsgMgr.getInstance().attach(this);
        binding.navigationMain.setOnNavigationItemSelectedListener(this);
        checkUpdate();

        // 如果api >= 23 需要显式申请权限
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_PHONE_STATE, Manifest.permission.INTERNET}, 0);
            }
        }
    }

    @Override
    protected void initData() {
        articlesFragment = new ArticlesFragment();
        taskFragment = new TaskFragment();
        mineFragment = new MineFragment();
        marketFragment = new MarketFragment();
        selectFragment(articlesFragment);
    }

    private void checkUpdate() {
        if (IntervalUtil.needCheckUpdate()) {
            UpgradeMgr.checkUpdate(this, false);
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        if (menuItem.isChecked()) return false;
        int itemId = menuItem.getItemId();
        if (itemId == R.id.main_navigation_home) {
            selectFragment(articlesFragment);
        } else if (itemId == R.id.main_navigation_square) {
            selectFragment(marketFragment);
        } else if (itemId == R.id.main_navigation_work) {
            selectFragment(taskFragment);
        } else if (itemId == R.id.main_navigation_mine) {
            selectFragment(mineFragment);
        }
        return true;
    }

    private BaseFragment current;

    private void selectFragment(BaseFragment fragment) {
        if (current == fragment) return;
//        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction()
//                .setCustomAnimations(R.anim.fade_in_800, R.anim.fade_out);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (fragment.isAdded()) {
            transaction.show(fragment).commit();
        } else {
            transaction.add(R.id.main_frameLayout, fragment).commit();
        }
        if (current != null) {
            transaction.hide(current);
        }
        current = fragment;
    }

    private void switchTask() {
        binding.navigationMain.setSelectedItemId(R.id.main_navigation_square);
    }

    @Override
    protected void onDestroy() {
        MsgMgr.getInstance().detach(this);
        super.onDestroy();
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Update_Info:
                if (articlesFragment != null) {
                    ((ArticlesFragment) articlesFragment).refreshView();
                }
                if (mineFragment != null) {
                    ((MineFragment) mineFragment).refreshData();
                }
                if (taskFragment != null) {
                    ((TaskFragment) taskFragment).refreshView();
                }
                break;
            case MsgType.MT_Switch_Task:
                switchTask();
                break;
            case MsgType.MT_Exchange_Suc:
                if (mineFragment != null) {
                    ((MineFragment) mineFragment).refreshMyArticles();
                }
                break;
        }
    }
}
